#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <URL> <segment_time> <airport>"
    exit 1
fi

URL=$1
SEGMENT_TIME=$2
AIRPORT=$3
ffmpeg -probesize 1000000 \
       -rtsp_transport tcp \
       -timeout 5000000 \
       -allowed_media_types video \
       -i $URL \
       -map 0:v \
       -an \
       -fps_mode:0 vfr \
       -c:v libx264 \
       -f segment \
       -segment_time $SEGMENT_TIME \
       -vf "scale=1920:1080" \
       -strftime 1 \
       $AIRPORT-%Y-%m-%d_%H-%M-%S.mp4 2>&1 | \
       gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0; fflush(); }' \
       | tee >(split -d -b 100M - "tmp/test_")